package com.tensquare.web.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class WebFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";//前置过滤器
    }

    @Override
    public int filterOrder() {
        return 0;//优先级为0，数字越大，优先级低
    }

    @Override
    public boolean shouldFilter() {
        return true;//是否执行该过滤器
    }

    /**
     * 过滤器内执行的操作，return中任何object值都会表示继续执行
     * setsendaullRespponse(false)表示不再执行
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
        System.out.println("webZuul过滤器。。。。");
        //得到上下文
        RequestContext currentContext = RequestContext.getCurrentContext();
        //得到requst域
        HttpServletRequest request = currentContext.getRequest();
        //得到头信息
        String authorization = request.getHeader("Authorization");
        if(authorization!=null && !"".equals(authorization)){
            currentContext.addZuulRequestHeader("Authorization",authorization);
        }
        System.out.println(authorization+"11111");
        return null;
    }
}
